<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>新增用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        body {
            background-image: url("/images/wallhaven.jpg");
            background-repeat: no-repeat;
            background-attachment: fixed;
            background-color: #e4eaf1;
            background-position: center;
        }
    </style>
</head>

<body>
<div class="layui-fluid" style="background-color:rgba(255,255,255,0.3);min-height: 427px">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input id="username" type="text" name="username" lay-verify="username" autocomplete="off"
                               placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label">密&#12288;码</label>
                    <div class="layui-input-block">
                        <input id="passwd" type="password" name="passwd" lay-verify="passwd" autocomplete="off"
                               placeholder="请输入密码" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label">重复密码</label>
                    <div class="layui-input-block">
                        <input id="repasswd" type="password" name="repasswd" lay-verify="repasswd" autocomplete="off"
                               placeholder="请再次输入密码" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input id="phone" type="text" name="phone" lay-verify="phone2" autocomplete="off"
                               placeholder="请输入手机号" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱地址</label>
                    <div class="layui-input-block">
                        <input id="email" type="text" name="email" lay-verify="email2|email" autocomplete="off"
                               placeholder="请输入邮件地址" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <button lay-filter="edit" lay-submit="" class="layui-btn layui-btn-fluid"
                        style="font-family:黑体;font-size:20px;height: 50px;border-radius: 15px">添加用户
                </button>
            </div>
        </form>
    </div>
</div>

<script>layui.use(['form', 'layer'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length === 0) {
                    return '请输入用户名！';
                } else if (value.length < 2) {
                    return '长度需大于等于2位'
                }
            },
            passwd: function (value) {
                if (value.length === 0) {
                    return '请输入密码';
                } else if (value.length < 4) {
                    return '密码长度应大于等于4位'
                }
            },
            repasswd: function (value) {
                if (value.length === 0) {
                    return '请确认密码';
                } else if ($('#passwd').val() !== $('#repasswd').val()) {
                    return '两次密码不相同！'
                }
            },
            email2: function (value) {
                if (value.length === 0) {
                    return '请输入邮箱！';
                }
            },
            phone2: function (value) {
                if (value.length === 0) {
                    return '请输入手机号！';
                }
            }
        });

        //监听提交
        form.on('submit(edit)',
            function (data) {
                // console.log(data.field);
                layer.msg('正在添加……', {time: 600});
                var load = layer.load();
                //使用ajax传递数据
                senddata = $.ajax({
                    type: "POST",
                    url: "/index/Tools/user_insert",
                    data: {
                        username: data.field.username,
                        passwd: data.field.passwd,
                        phone: data.field.phone,
                        email: data.field.email

                    },
                    success: function (msg) {
                        layer.close(load);
                        parent.parent.layui.notice.remove();
                        if (msg === 'ok') {
                            parent.parent.layui.notice.success('用户：' + data.field.username + '新增成功！', '操作完成', parent.parent.noticeOpt4);
                            parent.layer.msg('新增用户成功！', {
                                icon: 6,
                                time: 1200
                            });
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);

                            //刷新页面
                            setTimeout(function () {
                                parent.layer.close(index);
                                // parent.location.reload();
                                parent.$("#refresh").click();
                            }, 1000);

                        } else {
                            parent.parent.layui.notice.error(msg, '操作异常', parent.parent.noticeOpt5);
                            layer.alert(msg, {icon: 7});
                        }
                    },
                    error: function () {
                        layer.close(load);
                        parent.parent.layui.notice.remove();
                        parent.parent.layui.notice.error('数据提交失败，请联系后台管理员', '操作异常', parent.parent.noticeOpt5);
                        layer.alert('数据提交失败，请联系后台管理员！', {icon: 2})
                    }
                });

                return false;
            });
    });</script>
</body>
</html>